package com.dfbz.bigdata.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfbz.bigdata.common.utils.R;
import com.dfbz.bigdata.modules.system.doman.CommonAuth;

import com.dfbz.bigdata.modules.system.doman.CommonComputer;
import com.dfbz.bigdata.modules.system.service.CommonAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Consumer;

/**
 * @Auther:东方标准
 * @Date:2022/11/5-11-05-14:34
 * @Decsription:com.dfbz.bigdata.modules.system.controller
 * @Version:1.0
 */

@RestController
@RequestMapping("/auth")
@Slf4j
public class CommonAuthController {
    @Autowired
    private CommonAuthService commonAuthService;


    //    获得list
    @GetMapping
    public R<Page<CommonAuth>> getComputerList(String findMsg, int currentPage, int pagesizes) {
        Page<CommonAuth> page = new Page<>(currentPage, pagesizes);

        LambdaQueryWrapper<CommonAuth> lqw = new LambdaQueryWrapper();
        lqw.eq(CommonAuth::getAuthUserPass, 0).and(qr -> qr.like(CommonAuth::getAuthMsg, findMsg).or().like(CommonAuth::getAuthText, findMsg));
        commonAuthService.page(page, lqw);

        return R.success(page);
    }
    //    获得list
    @GetMapping("pass")
    public R<Page<CommonAuth>> getComputerPassList(String findMsg, int currentPage, int pagesizes) {
        Page<CommonAuth> page = new Page<>(currentPage, pagesizes);

        LambdaQueryWrapper<CommonAuth> lqw = new LambdaQueryWrapper();
        lqw.eq(CommonAuth::getAuthUserPass, 1).and(qr -> qr.like(CommonAuth::getAuthMsg, findMsg).or().like(CommonAuth::getAuthText, findMsg));
        commonAuthService.page(page, lqw);

        return R.success(page);
    }

    //    获得list
    @GetMapping("unpass")
    public R<Page<CommonAuth>> getComputerUnPassList(String findMsg, int currentPage, int pagesizes) {
        Page<CommonAuth> page = new Page<>(currentPage, pagesizes);

        LambdaQueryWrapper<CommonAuth> lqw = new LambdaQueryWrapper();
        lqw.eq(CommonAuth::getAuthUserPass, 2).and(qr -> qr.like(CommonAuth::getAuthMsg, findMsg).or().like(CommonAuth::getAuthText, findMsg));
        commonAuthService.page(page, lqw);

        return R.success(page);
    }



    @PostMapping("insert")
    public R<String> insert(@RequestBody CommonAuth commonAuth){

//        log.info("----------------------{}",commonAuth);
        String authUserId = commonAuth.getAuthUserId();
        System.out.println(authUserId);
        LambdaQueryWrapper<CommonAuth> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CommonAuth::getAuthUserId, authUserId);
        CommonAuth one = commonAuthService.getOne(lqw);
        if (one == null) {
            boolean save = commonAuthService.save(commonAuth);
            if (save) {
                return R.success("新增成功");
            }
            return R.error("服务器繁忙");
        } else {
            return R.error("有相同字段");
        }
    }

//    更新状态
    @GetMapping("update")
    public R<String> updateStatus(Long authId ,Integer status){

        UpdateWrapper<CommonAuth> lqw =new UpdateWrapper<>();
        lqw.lambda().eq(CommonAuth::getAuthId,authId);
        lqw.lambda().set(CommonAuth::getAuthUserPass,status);

        boolean flag = commonAuthService.update(lqw);
        if(flag){

            return R.success("更新状态成功");
        }else {
            return  R.error("服务器繁忙！！");
        }


    }



}
